package com.dongdong.service.impl;

import com.dongdong.mapper.TbTypeTemplateMapper;
import com.dongdong.model.TbSpecification;
import com.dongdong.model.TbSpecificationExample;
import com.dongdong.model.TbTypeTemplate;
import com.dongdong.model.TbTypeTemplateExample;
import com.dongdong.page.PageParams;
import com.dongdong.service.TypeTemplateService;
import com.dongdong.vo.SpecVO;
import com.dongdong.vo.TypeVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TypeTemplateServiceImpl implements TypeTemplateService {

    @Autowired
    private TbTypeTemplateMapper typeTemplateMapper;

    @Override
    public PageParams<TypeVO> getTypeList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TbTypeTemplate> typelist = typeTemplateMapper.selectByExample(null);
        PageInfo<TbTypeTemplate> info = new PageInfo<>(typelist);
        List<TypeVO> collect = typelist.stream().map(type -> {
            TypeVO typeVO = new TypeVO();
            BeanUtils.copyProperties(type, typeVO);
            return typeVO;
        }).collect(Collectors.toList());
        return new PageParams<>(info.getPageNum(), info.getPageSize(), collect, info.getTotal(), info.getPages());
    }

    @Override
    public TypeVO getTypeById(Long id) {
        TbTypeTemplate tbTypeTemplate = typeTemplateMapper.selectByPrimaryKey(id);
        if (tbTypeTemplate == null){
            return null;
        }
        TypeVO vo = new TypeVO();
        BeanUtils.copyProperties(tbTypeTemplate, vo);
        return vo;
    }

    @Override
    public int updateType(TypeVO typeVO) {
        TbTypeTemplate tbTypeTemplate = new TbTypeTemplate();
        BeanUtils.copyProperties(typeVO,tbTypeTemplate);
        int i = typeTemplateMapper.updateByPrimaryKeySelective(tbTypeTemplate);
        return i;
    }

    @Override
    public List<TypeVO> findTypeList(TypeVO typeVO) {
        TbTypeTemplateExample example = new TbTypeTemplateExample();
        if (typeVO.getName() != null){
            example.createCriteria().andNameLike("%"+typeVO.getName()+"%");
        }
        List<TbTypeTemplate> speclist = typeTemplateMapper.selectByExample(example);
        List<TypeVO> collect = speclist.stream().map(spec -> {
            TypeVO vo = new TypeVO();
            BeanUtils.copyProperties(spec, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public int InsertType(TypeVO typeVO) {
        TbTypeTemplate tbTypeTemplate = new TbTypeTemplate();
        BeanUtils.copyProperties(typeVO,tbTypeTemplate);
        int i = typeTemplateMapper.insert(tbTypeTemplate);
        return i;
    }

    @Override
    public int deleteType(Long[] arr) {
        TbTypeTemplateExample example = new TbTypeTemplateExample();
        example.createCriteria().andIdIn(Arrays.asList(arr));
        int i = typeTemplateMapper.deleteByExample(example);
        return i;
    }

    @Override
    public List<TypeVO> getTypeListAll() {
        List<TbTypeTemplate> typelist = typeTemplateMapper.selectByExample(null);
        List<TypeVO> collect = typelist.stream().map(type -> {
            TypeVO vo = new TypeVO();
            BeanUtils.copyProperties(type, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }
}
